package com.dodoke.interceptor;

import com.dodoke.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle....");
        // 判断用户是否登录，就是判断session中是否存在user对象
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if(user != null){
            return true;
        }
        // 重定向到登录页
        response.sendRedirect(request.getContextPath() + "/login.jsp");
        return false;
    }
}
